package edu.gatech.aristotle.random.experiment;

import java.util.ArrayList;

import edu.gatech.aristotle.random.util.CONSTANT_FILE_NAME;
import edu.gatech.aristotle.random.util.CONSTANT_PRINT_TOKEN_INPUT_SPACE;
import edu.gatech.aristotle.random.util.Util_FormatHandler;

import net.util.File_Handler;

public class MultipleBugExperiment implements CONSTANT_PRINT_TOKEN_INPUT_SPACE, CONSTANT_FILE_NAME  {
	
	public ArrayList<String> findResultLines( String fileName ){

		ArrayList<String> resultLines	= File_Handler.fileContentToStrings( fileName );
		
		return resultLines;
	}
															
	/** the sum of each line of integer */
	public ArrayList<Integer> readInTestResult( String fileName ){
		ArrayList<Integer> sumOfLines		= new ArrayList<Integer>();
		
		ArrayList<String> resultLines	= findResultLines( fileName );
		
		for ( String line : resultLines ){
			ArrayList<Integer> resultIntegers	= Util_FormatHandler.oneStringToInteger( line );
			
			int sumOfInt	= Util_FormatHandler.sumIntegerList( resultIntegers );
			
			sumOfLines.add( sumOfInt );
		}
		
		return sumOfLines;
	}
	
	public ArrayList<Integer> findNVersionFailedIndice( int n, String fileName ){
		ArrayList<String> resultLines	= findResultLines( fileName );
		
		return this.findNVersionFailedIndice( n,  resultLines);
	}
	
	
	public ArrayList<Integer> findAllFailedIndice( String fileName ){
		ArrayList<String> resultLines	= findResultLines( fileName );
		
		return this.findAllFailedIndice( resultLines );
	}
	
	private  ArrayList<Integer> findNVersionFailedIndice( int n, ArrayList<String> resultLines ){
		/** lastVersion is the all-bugs version */
		String lastVersion	= resultLines.remove( resultLines.size() - 1 );
		
		ArrayList<Integer> failedIndice = new ArrayList<Integer>();
		int totalIndices	= resultLines.get( 0 ).length();
		int totalVersion	= resultLines.size();
		
		for ( int i = 0; i < totalIndices; i++ ){
			int failedVersion	= 0;
			
			for ( int j = 0; j < totalVersion - 1; j++ ){
				char jth	= resultLines.get( j ).charAt( i );
				char jNext	= resultLines.get( j + 1 ).charAt( i );
				char last	= lastVersion.charAt( i );
				if ( ( jth == jNext ) && ( jth == last ) && ( jth == '1' ) ) failedVersion++;
			} 
			
			if ( failedVersion == (n-1) )	failedIndice.add( i );
		}
		
		return failedIndice;
	}
	
	private  ArrayList<Integer> findAllFailedIndice( ArrayList<String> resultLinesArrayList ){
		return findNVersionFailedIndice( resultLinesArrayList.size(), resultLinesArrayList);
	}
}
